import {
	HttpWidget,
	Http
} from '../dt-request'
import common from './common.js'

const createPay = (group, data) => {
	return new Promise((resolve, reject) => {
		let http = new Http()
		http.post('pay', {
				pay_type: 1,
				trade_type: 'mini_program',
				order_group: group,
				data: JSON.stringify(data)
			})
			.then((response) => {
				console.log(response.data.data)
				let config = response.data.data.config
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: config.timestamp,
					nonceStr: config.nonceStr,
					package: config.package,
					signType: config.signType,
					paySign: config.paySign,
					success: (response) => {
						resolve(response)
					},
					fail: (error) => {
						reject(error)
					}
				})
			})
			.catch((response) => {
				reject(response)
			})
	})
}

const payOrder = (order_id, group = 'order') => {
	return new Promise((resolve, reject) => {
		let http = new Http()
		http.post('pay', {
				pay_type: 1,
				trade_type: 'mini_program',
				order_group: group,
				data: JSON.stringify({
					"order_id": order_id
				})
			})
			.then((response) => {
				console.log(response)
				let config = response.data.data.config
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: config.timestamp,
					nonceStr: config.nonceStr,
					package: config.package,
					signType: config.signType,
					paySign: config.paySign,
					success: (response) => {
						resolve(response)
					},
					fail: (error) => {
						reject(error)
					}
				})
			})
			.catch((response) => {
				reject(response)
			})
	})
}

export default {
	payOrder,
	createPay
}
